﻿#include "add_picture.h"
#include "ui_add_picture.h"

#include <qmath.h>

add_picture::add_picture(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::add_picture)
{
    ui->setupUi(this);
    setMouseTracking(true);//开启鼠标实时追踪，监听鼠标移动事件，默认只有按下时才监听
    //pix = QPixmap(900, 900);
    pix = QPixmap(this->width(),this->height()/2);
    pix.fill(Qt::white);
    drawtype = 1;

    ui->tableWidget->setColumnCount(2);
    QStringList header;
    header<<"number"<<"described";
    ui->tableWidget->setHorizontalHeaderLabels(header);
    ui->tableWidget->verticalHeader()->setVisible(false);

    ui->tableWidget->setColumnWidth(0,ui->tableWidget->width()/3);
    ui->tableWidget->setColumnWidth(1,ui->tableWidget->width()/3*2);

}

add_picture::~add_picture()
{
    delete ui;
}


void add_picture::paintEvent(QPaintEvent *)
{
//    QWidget::paintEvent(e);
    //qDebug()<<this->width()<<this->height();
    int count;
    int scale = 5;
    if(flag1 == 0)  //新建模式
        count = a_all_store->picture_count;
    if(flag1 == 1)  //选中模式
        count = cur_sel;

    {
        QPainter painter(this);
        QPainter pp(&pix);
        pp.drawRect(QRect(0,0,this->width()-1,this->height()/2-1));
        pp.setPen(QPen(Qt::DashLine));   //设置画笔为
        pp.setPen(QPen(Qt::yellow));
        for(int i = 100;i<this->width()-1;i=i+100)
            pp.drawLine(QPoint(i,0),QPoint(i,this->height()));
        for(int i = 100;i<this->height()-1;i=i+100)
            pp.drawLine(QPoint(0,i),QPoint(this->width(),i));
        //pp.drawRect(50,50,100,100);
        painter.drawPixmap(0, 0, pix);
    }
    if(drawtype == 1)  //画矩形
    {
        int x = lastPoint.x();
        int y = lastPoint.y();
        int w = endPoint.x()-x;
        int h = endPoint.y()-y;

        QPainter painter(this);
        if(drawing == 1)
        {
            tmp_pix=pix;
            QPainter pp(&tmp_pix);
            pp.drawRect(QRect(x,y,w,h));
            painter.drawPixmap(0, 0, tmp_pix);
        }
        else if(drawing == 0)
        {
            QPainter pp(&pix);
            pp.drawRect(QRect(x,y,w,h));
//            painter.drawRect(QRect(x,y,w,h));
            painter.drawPixmap(0, 0, pix);

            storerect temp_rect = {h/scale, w/scale, x/scale, y/scale};  //临时替代
            a_all_store->p_picture[count].p_rect.push_back(temp_rect);
        }
    }

    else if(drawtype == 2)   //画椭圆
    {
        QPainter painter(this);
        int x = lastPoint.x();
        int y = lastPoint.y();
        int w = endPoint.x()-x;
        int h = endPoint.y()-y;
        if(drawing == 1)  //正在画
        {
            tmp_pix=pix;
            QPainter pp(&tmp_pix);
            pp.drawEllipse( QPoint(x,y) ,w, h);
            //pp.drawEllipse(x,y,w, h);
            painter.drawPixmap(0, 0, tmp_pix);
        }
        else if(drawing == 0) //结束
        {
            //qDebug()<<lastPoint<<" "<<endPoint<<"椭圆画下";
            QPainter pp(&pix);
            pp.drawEllipse(QPoint(x,y) ,w, h);
            //pp.drawEllipse(x,y ,w, h);
            painter.drawPixmap(0, 0, pix);
            //storeellipse temp_ellipse = {w,h,100,150};
            storeellipse temp_ellipse = {w/scale, h/scale, x/scale, y/scale};   //临时变量
            a_all_store->p_picture[count].p_ellipse.push_back(temp_ellipse);
        }
    }
    else if(drawtype == 3) //画直线
    {
         QPainter painter(this);
         if(drawing == 1)  //正在画
         {
             tmp_pix=pix;
             QPainter pp(&tmp_pix);
             pp.drawLine(tmpoint,endPoint);
             painter.drawPixmap(0, 0, tmp_pix);
         }
         else if(drawing == 0) //结束
         {
             QPainter pp(&pix);
             pp.drawLine(tmpoint,endPoint);
             painter.drawPixmap(0, 0, pix);
             QPoint point1 = tmpoint/scale;
             QPoint point2 = endPoint/scale;
             storeline temp_line = {point1,point2};
             a_all_store->p_picture[count].p_line.push_back(temp_line);
         }
    }

    else if(drawtype == 6)
    {
        QPainter painter(this);
        if(drawing == 1 || drawing == 3 || drawing == 4)  //曲线确定首末两点
        {
            tmp_pix=pix;
            QPainter pp(&tmp_pix);

            QPainterPath myPath;
            myPath.moveTo(lastPoint);
            myPath.cubicTo(tmpoint, tmpoint1, endPoint);
            //pp.setPen(Qt::red);
            pp.drawPath(myPath);
            painter.drawPixmap(0, 0, tmp_pix);
        }
        else if(drawing == 0)
        {
            QPainter pp(&pix);
            QPainterPath myPath;
            myPath.moveTo(lastPoint);
            myPath.cubicTo(tmpoint, tmpoint1, endPoint);
            pp.drawPath(myPath);
            painter.drawPixmap(0, 0, pix);
            storecurve temp_curve = {lastPoint/scale,tmpoint/scale,tmpoint1/scale,endPoint/scale};
            a_all_store->p_picture[count].p_curve.push_back(temp_curve);
        }
    }
//    QPainter painter(this);
//    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
//    painter.drawPixmap(0, 0, m_pixmap);
}

//void add_picture::updacanvasPixmap(const patternstore &all_store)
void add_picture::updacanvasPixmap(patternstore *all_store)
{
    //a_all_store = all_store;   //接收外来的图案
    a_all_store = all_store;
    QPixmap pixmap(this->width(),this->height()/2);
    pixmap.fill(Qt::white);
    QPainter painter(&pixmap);
//    painter.translate(this->width()/2,this->height()/2);  //提前旋转一个坐标系
//    for(int j = 0;j<all_store.p_text.size();j++)
//    {
//        QString content = all_store.p_text[j].text;
//        qreal q = all_store.p_text[j].angle;  //旋转角度
//        qreal l = 0;
//        QFont font = painter.font();
//        font.setFamily("Microsoft YaHei");
//        font.setPointSize(all_store.p_text[j].font_size);   //设置字体大小
//        font.setWeight(99);   // 调整字体粗细
//        painter.setFont(font);

//        for(int i = 0; i < content.length(); i++)
//        {
//            painter.save();
//            double radian = q*M_PI/180;
//            //qDebug()<<radian;
//            painter.translate(all_store.p_text[j].radius*cos(radian),all_store.p_text[j].radius*sin(radian));
//           // painter.translate(0,10);
//            //painter.rotate(q + 95);
//            painter.rotate(q+90);
//            const QChar && charTxt = content.at(i);
//            l = painter.fontMetrics().width(charTxt) / 2.0;
//            //qDebug()<<painter.fontMetrics().width(charTxt);
//            painter.drawText(QPoint(-l,0),charTxt);
//            q = q+10;
//            painter.restore();
//        }

//        painter.save();
//        painter.setBrush(QColor(0,170,255,100));
//        painter.restore();

//    }
//    painter.drawEllipse(QPoint(30,30),30,30);
    pix = pixmap;
    //update();
}




void add_picture::mousePressEvent(QMouseEvent *event)
{
     //鼠标左键按下
    if(event->button() == Qt::LeftButton & (drawtype == 1 || drawtype == 2 || drawtype == 3))
    {
           qDebug()<<"鼠标按下"<<drawtype;
            lastPoint = event->pos();
            endPoint = lastPoint;
            tmpoint = lastPoint;
            qDebug()<<lastPoint<<" "<<endPoint<<"左键按下";
            drawing = 1;
    }
    else if(event->button() == Qt::LeftButton && drawtype == 6)
    {
        if(drawing == 2)
        {
             lastPoint = event->pos();
             endPoint = lastPoint;
             tmpoint = endPoint;
             tmpoint1 = endPoint;
             qDebug()<<lastPoint<<" "<<endPoint<<"曲线鼠标按下1";
             drawing = 1;
        }
        else if(drawing == 3) //确定第一个拐点
        {
            tmpoint = event->pos();
            //drawing = 1;
        }
        else if(drawing == 4) //确定第二个拐点
        {
            tmpoint1 = event->pos();
        }
    }

}

void add_picture::mouseMoveEvent(QMouseEvent *event)
{
    // 鼠标左键按下的同时移动鼠标

    if(event->buttons() & Qt::LeftButton & (drawtype == 1 | drawtype == 2
                             | drawtype == 3 | drawtype == 4 | drawtype == 5))
    {
       drawing = 1;
       endPoint = event->pos();
       //qDebug()<<lastPoint<<" "<<endPoint<<"左键移动";
       update();//进行绘制
    }
    else if(event->buttons() && Qt::LeftButton && (drawtype == 6) )
    {
        if(drawing == 1)
        {
            endPoint = event->pos();
            tmpoint = endPoint;
            tmpoint1 = endPoint;
            qDebug()<<lastPoint<<" "<<endPoint<<"左键移动";
            update();//进行绘制
        }
        else if(drawing == 3)  //绘制第一个拐点
        {
            tmpoint = event->pos();
            update();
        }
        else if(drawing == 4)  //绘制第二个拐点
        {
            tmpoint1 = event->pos();
            update();
        }
    }
}

void add_picture::mouseReleaseEvent(QMouseEvent *event)
{
        if(event->button() == Qt::LeftButton && (drawtype == 1 | drawtype == 2
                                   | drawtype == 3 | drawtype == 4 | drawtype == 5)) //鼠标左键释放
        {
            endPoint = event->pos();
            drawing = 0;
            qDebug()<<"释放鼠标";
            update();
           // lastPoint = endPoint;

        }
        else if(event->button() == Qt::LeftButton && drawtype == 6)
        {
            if(drawing == 1)
            {
                endPoint = event->pos();
                drawing = 3;   //曲线绘画进入确定第一个拐点
                qDebug()<<"释放鼠标1";
                update();
            }
            else if(drawing == 3)
            {
                tmpoint = event->pos();
                drawing = 4;   //曲线绘画进入确定第二个拐点
                qDebug()<<"释放鼠标2";
                update();
            }
            else if(drawing == 4)  //最后结束
            {
                tmpoint1 = event->pos();
                drawing = 0;
                update();
            }
        }
}

void add_picture::on_rectButton_clicked()
{
    drawtype = 1;
    drawing = 2;
}


void add_picture::on_ellipseButton_clicked()
{
    drawtype = 2;
    drawing = 2;
}

void add_picture::on_lineButton_clicked()
{
    drawtype = 3;
    drawing = 2;
}

void add_picture::on_curveButton_clicked()
{
    drawtype = 6;   //曲线
    drawing = 2;  //待机
}


void add_picture::on_saveButton_clicked()
{
    if(flag1 == 0)   //新建图案状态
    {
        a_all_store->p_picture[a_all_store->picture_count].p_text = ui->Picture_text->toPlainText();  //将文字弄进结构体

        a_all_store->picture_count = a_all_store->picture_count + 1;
    }

    else     //选中状态
    {
        flag1 = 0;  //回到新建图案状态
    }

    ui->Picture_text->setText("");

    this->on_tableWidget_itemSelectionChanged();

    drawing = 2;
    pix.fill(Qt::white);
    tmp_pix.fill(Qt::white);
    repaint();
    update();
}


void add_picture::on_tableWidget_itemSelectionChanged()
{
    QTableWidgetItem *item[2];
    int i,nCount;
    qDebug()<<"a_all_store.picture_count:"<<a_all_store->picture_count;

    for(i=0; i<a_all_store->picture_count; i++)
    {
        nCount = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(nCount);

//        QString strText = ui->tableWidget->item(i,1)->text();
//        qDebug()<<strText<<"okk";

        item[0] = new QTableWidgetItem(QString("%1").arg(i+1));
        item[1] = new QTableWidgetItem(a_all_store->p_picture[i].p_text);
        ui->tableWidget->setItem(i, 0, item[0]);
        ui->tableWidget->setItem(i, 1, item[1]);
    }
}


void add_picture::on_tableWidget_cellClicked(int row, int column)
{
    flag1 = 1;
    qDebug()<<row<<column;
    QString strText = ui->tableWidget->item(row, column)->text();
    qDebug()<<strText;
    cur_sel = row;
    get_picture();
}


void add_picture::get_picture()   //从图案库获取图案
{
    QPixmap pixmap(900,900);
    pixmap.fill(Qt::white);
    QPainter painter(&pixmap);
    qDebug()<<cur_sel;
    //qDebug()<<a_all_store->p_picture[cur_sel].p_rect[0].x<<a_all_store->p_picture[cur_sel].p_rect[0].y<<cur_sel;
    for(int j = 0;j<a_all_store->p_picture[cur_sel].p_rect.size();j++)
    {
        int x = a_all_store->p_picture[cur_sel].p_rect[j].x;
        int y = a_all_store->p_picture[cur_sel].p_rect[j].y;
        int w = a_all_store->p_picture[cur_sel].p_rect[j].width;
        int h = a_all_store->p_picture[cur_sel].p_rect[j].height;
        painter.drawRect(x,y,w,h);
    }
    for(int j = 0;j<a_all_store->p_picture[cur_sel].p_ellipse.size();j++)
    {
        int x = a_all_store->p_picture[cur_sel].p_ellipse[j].x;
        int y = a_all_store->p_picture[cur_sel].p_ellipse[j].y;
        int w = a_all_store->p_picture[cur_sel].p_ellipse[j].width;
        int h = a_all_store->p_picture[cur_sel].p_ellipse[j].length;
        painter.drawEllipse(QPoint(x,y),h,w);
    }

//    painter.drawEllipse(QPoint(0,0),300,300);
    pix = pixmap;
    update();
}

